package cn.iocoder.yudao.module.design.service.taskactionInterface;

import cn.hutool.core.convert.Convert;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.design.controller.admin.relativednc.vo.RelativedNcDealVO;
import cn.iocoder.yudao.module.design.dal.dataobject.flowtask.FlowTaskDO;
import cn.iocoder.yudao.module.design.dal.dataobject.relatived.RelativedDO;
import cn.iocoder.yudao.module.design.dal.dataobject.relativednc.RelativedNcDO;
import cn.iocoder.yudao.module.design.dal.mysql.flowtask.FlowTaskMapper;
import cn.iocoder.yudao.module.design.dal.mysql.relatived.RelativedMapper;
import cn.iocoder.yudao.module.design.dal.mysql.relativednc.RelativedNcMapper;
import cn.iocoder.yudao.module.design.enums.design.FlowTaskStatusEnum;
import cn.iocoder.yudao.module.design.enums.design.FlowTaskTypeEnum;
import cn.iocoder.yudao.module.design.enums.design.RelativedNcStatusCdEnum;
import cn.iocoder.yudao.module.design.enums.design.RelativedStatusCdEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 好友请求处理
 */
@Service
@Validated
@Slf4j
public class RelativeTaskActionImpl implements  FlowTaskActionInterface{
    @Resource
    private RelativedNcMapper relativedNcMapper;

    @Resource
    private RelativedMapper relativedMapper;

    @Resource
    private FlowTaskMapper flowTaskMapper;
    /**
     * 任务处理-同意-流转
     */
    @Override
    public void confirmAction(Object obj) {
        final RelativedNcDealVO vo = Convert.convert(RelativedNcDealVO.class, obj);
        // 查询 请求是否存在   当前人id    userId  待处理  角色id
        final String relId = SecurityFrameworkUtils.getLoginUserId().toString();
        QueryWrapper query = new QueryWrapper();
        query.eq("user_id",vo.getUserId());
        query.eq("rel_id",relId);
        query.eq("status_cd",RelativedNcStatusCdEnum.USER_REL_STATUS_CD_0.getCode().toString());

        List<RelativedNcDO> relativedNcList = relativedNcMapper.selectList(query);
        // 不存在 则数据不完整报错
        if(relativedNcList.size()<=0){
            throw  new NullPointerException("数据不正确，请检查");
        }

        //  发起人正向数据
        for (RelativedNcDO relativedNcDO:relativedNcList
        ) {
            RelativedDO relativedDO = new RelativedDO();
            relativedDO.setNickName(relativedNcDO.getRemarkName());
            relativedDO.setUserId(relativedNcDO.getUserId());
            relativedDO.setRelUserId(relId);
            relativedDO.setRelationStatus(RelativedStatusCdEnum.USER_REL_STATUS_CD_1.getCode().toString());
            relativedDO.setRelUserCd(relativedNcDO.getRoleId());
            relativedMapper.insert(relativedDO);
        }


        //  修改 relativedNc 中状态 处理完成时间
        QueryWrapper updateWrapper = new QueryWrapper();
        // set
        RelativedNcDO  setClounm = new RelativedNcDO();
        setClounm.setStatusCd(RelativedNcStatusCdEnum.USER_REL_STATUS_CD_1.getCode().toString());
        setClounm.setFinishTime(new Date());
//        条件
        updateWrapper.eq("user_id",vo.getUserId());
        updateWrapper.eq("rel_id",relId);
        updateWrapper.eq("status_cd",RelativedNcStatusCdEnum.USER_REL_STATUS_CD_0.getCode().toString());
        relativedNcMapper.update(setClounm,updateWrapper);

        // 判断是否有相关关联的任务 在开启
        // 判断是否有待办任务是否待开始  有则改为开始
        FlowTaskDO flowTaskDO1 = new FlowTaskDO();
        flowTaskDO1.setStatusCd(FlowTaskStatusEnum.FLOW_STATUS_ENUM_1.getCode());
        QueryWrapper<FlowTaskDO> flowTaskDOQueryWrapper = new QueryWrapper<>();
        // 查询待开始的待办任务 发起人为 申请人的 处理人为当前登录人的
        flowTaskDOQueryWrapper.eq("creator",vo.getUserId());
        flowTaskDOQueryWrapper.eq("dealer_id",relId);
        // 项目关联任务
        flowTaskDOQueryWrapper.eq("task_type", FlowTaskTypeEnum.FLOW_TASk_CD_2.getCode());
        // 待开始
        flowTaskDOQueryWrapper.eq("status_cd",FlowTaskStatusEnum.FLOW_STATUS_ENUM_0.getCode());
        flowTaskMapper.update(flowTaskDO1,flowTaskDOQueryWrapper);
    }

    /**
     * 任务处理-拒绝-回退
     *
     */
    @Override
    public void refuseAction(Object obj) {
        final RelativedNcDealVO vo = Convert.convert(RelativedNcDealVO.class, obj);
        // 查询 请求是否存在   当前人id    userId  待处理  角色id
        final String relId = SecurityFrameworkUtils.getLoginUserId().toString();
        QueryWrapper query = new QueryWrapper();
        query.eq("user_id",vo.getUserId());
        query.eq("rel_id",relId);
        query.eq("status_cd",RelativedNcStatusCdEnum.USER_REL_STATUS_CD_0.getCode().toString());

        List<RelativedNcDO> relativedNcList = relativedNcMapper.selectList(query);
        // 不存在 则数据不完整报错
        if(relativedNcList.size()<=0){
            throw  new NullPointerException("数据不正确，请检查");
        }

        //  发起人正向数据
        for (RelativedNcDO relativedNcDO:relativedNcList
        ) {
            RelativedDO relativedDO = new RelativedDO();
            relativedDO.setNickName(relativedNcDO.getRemarkName());
            relativedDO.setUserId(relativedNcDO.getUserId());
            relativedDO.setRelUserId(relId);
            relativedDO.setRelationStatus(RelativedStatusCdEnum.USER_REL_STATUS_CD_2.getCode().toString());
            relativedDO.setRelUserCd(relativedNcDO.getRoleId());
            relativedMapper.insert(relativedDO);
        }

    }

    /**
     * 任务处理-查看
     *
     * @param obj
     */
    @Override
    public Object viewAction(Object obj) {
        return null;
    }

    /**
     * 任务补充
     *
     * @param obj
     */
    @Override
    public void replenishTask(Object obj) {

    }
}
